package de.hs.rm.UJAdapter.impl;

import java.util.Dictionary;
import java.util.Hashtable;

import javax.management.Attribute;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

import org.osgi.service.upnp.UPnPAction;
import org.osgi.service.upnp.UPnPStateVariable;

public class UJAction implements UPnPAction {

	protected String name = null;
	protected String returnArgumentName = null;
	protected String[] inputArgumentsNames = null;
	protected String[] outputArgumentsNames = null;
	protected String[] inputArgumentsTypes = null;
	protected String[] outputArgumentsTypes = null;
	protected UJStateVariable stateVariable = null;
	protected ObjectName objName = null;
	protected MBeanServerConnection mBeanServerConnection = null;
	protected String javaName = null;
	
	protected Hashtable stateVariables = null;
	
	@Override
	public String getName() {
		return name;
	}

	@Override
	public String getReturnArgumentName() {
		return returnArgumentName;
	}

	@Override
	public String[] getInputArgumentNames() {
		return inputArgumentsNames;
	}

	@Override
	public String[] getOutputArgumentNames() {
		return outputArgumentsNames;
	}

	@Override
	public UPnPStateVariable getStateVariable(String argumentName) {
		
		return (UPnPStateVariable)stateVariables.get(argumentName);
	}

	@Override
	public Dictionary invoke(Dictionary args) throws Exception {
		
		Object result = null;
		if(inputArgumentsNames.length > 0)
		{
			Object[] params = new Object[inputArgumentsNames.length];
			
			for (int i = 0; i < inputArgumentsNames.length; i++)
				params[i] = args.get(inputArgumentsNames[i]);
			
			result = mBeanServerConnection.invoke(objName,
					this.getJavaName(),
					params,
					inputArgumentsTypes);
		}
		else
		{
			result = mBeanServerConnection.invoke(objName,
					this.getJavaName(),
					null,
					null);
			
		}
		//mBeanServerConnection.invoke(objName, name, params, null);
		
		if(result != null)
		{
			Dictionary returnValue = new Hashtable();
			returnValue.put(outputArgumentsNames[0], result);
			return returnValue;
		}
		else
			return null;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setReturnArgumentName(String returnArgumentName) {
		this.returnArgumentName = returnArgumentName;
	}

	public void setInputArgumentsNames(String[] inputArgumentsNames) {
		this.inputArgumentsNames = inputArgumentsNames;
	}

	public void setOutputArgumentsNames(String[] outputArgumentsNames) {
		this.outputArgumentsNames = outputArgumentsNames;
	}

	public void setStateVariable(UJStateVariable stateVariable) {
		this.stateVariable = stateVariable;
	}

	public void setmBeanServerConnection(MBeanServerConnection mBeanServerConnection) {
		this.mBeanServerConnection = mBeanServerConnection;
	}

	public void setObjName(ObjectName objName) {
		this.objName = objName;
	}
	
	public void setInputArgumentsTypes(String[] inputArgumentsTypes) {
		this.inputArgumentsTypes = inputArgumentsTypes;
	}

	public void setOutputArgumentsTypes(String[] outputArgumentsTypes) {
		this.outputArgumentsTypes = outputArgumentsTypes;
	}

	public void addStateVariable(String stateVariableName, UJStateVariable stateVariable) {
		
		if (this.stateVariables == null)
			this.stateVariables = new Hashtable();
		stateVariables.put(stateVariableName, stateVariable);
	}
	
	public String getJavaName() {
		return javaName;
	}

	public void setJavaName(String javaName) {
		this.javaName = javaName;
	}

}
